package benny_driving_ser.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import atg.taglib.json.util.JSONArray;
import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;
import benny_driving_ser.dao.impl.AccountDaoImpl;
import benny_driving_ser.entity.DriConRecs;
import benny_driving_ser.entity.SessionAttributeDriId;
import benny_driving_ser.unionpay.action.Query;
import benny_driving_ser.util.SessionUtil;

public class AccountServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6528205191144309490L;

	/**
	 * Constructor of the object.
	 */
	public AccountServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action;
		if (null == (action = request.getParameter("action")))
			return;
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		if ("dri-zhcz".equals(action.trim())) {// 未完成
			SessionAttributeDriId sad = SessionUtil.getInstance()
					.verifySessionIdForDri(request);
			if (null != sad) {
				String cost;
				if (null == (cost = request.getParameter("cost")))
					return;
				String res = AccountDaoImpl.getInstance().driTopUp(
						Integer.parseInt(sad.getValue().trim()), cost.trim());
				out.print(res);
			} else {
				out.print(-1);
			}

		}

		if ("dri-acc".equals(action.trim())) {
			SessionAttributeDriId sad = SessionUtil.getInstance()
					.verifySessionIdForDri(request);
			if (null == sad)
				out.print(-1);
			else {
				String res = AccountDaoImpl.getInstance()
						.driAcc(sad.getValue());
				out.print(res);
			}
			// String driid;
			// if (null == (driid = request.getParameter("driid"))) {
			// out.close();
			// return;
			// }
		}
		if ("dri-xfjlcx".equals(action.trim())) {// 未完成
			String driid;
			if (null == (driid = request.getParameter("driid")))
				return;
			List<DriConRecs> dcrs = AccountDaoImpl.getInstance().getDriConRecs(
					Integer.parseInt(driid.trim()));
			if (dcrs != null) {
				JSONArray ja = new JSONArray();
				JSONObject jo = new JSONObject();
				try {
					for (DriConRecs dcr : dcrs) {
						JSONObject mJo = new JSONObject();
						mJo.put("xfformid", dcr.getXfformid());
						mJo.put("formid", dcr.getFormid());
						mJo.put("time", dcr.getTime());
						mJo.put("cost", dcr.getCost());
						mJo.put("project", dcr.getProject());
						ja.add(mJo);
					}
					jo.put("conrecs", ja);
					out.print(jo.toString());
				} catch (JSONException e) {
					e.printStackTrace();
					out.print("{'conrecs':[]}");
				}
			} else
				out.print("{'conrecs':[]}");
		}

		if ("query".equals(action.trim())) {
			String orderNum, orderTime;
			if (null == (orderNum = request.getParameter("orderNum"))
					|| null == (orderTime = request.getParameter("orderTime")))
				return;
			String res = Query.getInstance().unionQuery(orderTime, orderNum);
			out.print(res);
		}
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
